package com.chainyoung.component_tmtrade.ui.fragment.fiatcurrency;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.adpter.BaseFragmentAdapter;
import com.chainyoung.common.base.BaseFragment;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.event.BaseEvent;
import com.chainyoung.common.utils.CommonUtil;
import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.R2;
import com.chainyoung.component_tmtrade.bean.FiatCoin;
import com.chainyoung.component_tmtrade.di.component.DaggerTradeFiatCurrencyComponent;
import com.chainyoung.component_tmtrade.di.module.TradeFiatCurrencyModule;
import com.chainyoung.component_tmtrade.ui.activity.AboutFiatCurrencyActivity;
import com.chainyoung.component_tmtrade.ui.mvp.contract.TradeFiatCurrencyMainCurrencyContract;
import com.chainyoung.component_tmtrade.ui.mvp.presenter.TradeFiatCurrencyMainPresenter;
import com.chainyoung.component_tmtrade.ui.view.SelectCoinWindow;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;
import com.flyco.tablayout.SlidingTabLayout;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import q.rorbin.badgeview.QBadgeView;

import static com.chainyoung.component_tmtrade.ui.fragment.fiatcurrency.TradeFiatCurrencyOrderFragment.ORDER_CURRENT;
import static com.chainyoung.component_tmtrade.ui.fragment.fiatcurrency.TradeFiatCurrencyOrderFragment.ORDER_HISTORY;


@Route(path = RouterConstants.PATH_TRADE_FIAT_CURRENCY)
public class TradeFiatCurrencyMainFragment extends BaseFragment<TradeFiatCurrencyMainPresenter> implements TradeFiatCurrencyMainCurrencyContract.View {


    @BindView(R2.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R2.id.view_pager)
    ViewPager viewPager;

    private List<Fragment> fragmentList = new ArrayList<>();
    private List<FiatCoin> mCoinList = new ArrayList<>();

    public static final int TYPE_BUY = 1;
    public static final int TYPE_SELL = 2;
    private QBadgeView badgeView;

    private FiatCoin curfiatCoin;


    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerTradeFiatCurrencyComponent.builder()
                .appComponent(appComponent)
                .tradeFiatCurrencyModule(new TradeFiatCurrencyModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUi() {
        super.initUi();
        //showAndSetBarLeftText(getString(R.string.tmtrade_about_fiat_currency));
        setCurrentTitle(getString(R.string.tmtrade_fiat_currency));
        // mBarLeftTextView.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,R.mipmap.ic_arrow_right,0);
        // mBarLeftTextView.setCompoundDrawablePadding(8);
        showAndSetBarRightIcon(R.mipmap.ic_expand);
       // showAndSetBarRightLeftIcon(R.mipmap.ic_expand);
        initMsgNum();
    }

    private void initMsgNum() {
        int num = 0;
        if (CommonUtil.getUserInfo() != null) {
            num = CommonUtil.getUserInfo().getUnread_msg_num();
        }
        /*if (badgeView == null) {
            badgeView = new QBadgeView(mContext);
            badgeView.setBadgeBackgroundColor(ContextCompat.getColor(mContext, R.color._F95A54));
            badgeView.setBadgeTextColor(ContextCompat.getColor(mContext, R.color.white));
            badgeView.setBadgeTextSize(9, true);
            badgeView.bindTarget(mBarRightImageView).setBadgeGravity(Gravity.TOP | Gravity.END).
                    setGravityOffset(4, 3, true).
                    setBadgeNumber(num);
        } else {
            badgeView.setBadgeNumber(num);
        }*/
    }

    @Override
    public void onLeftTextViewClicked(View view) {
        super.onLeftTextViewClicked(view);
       // launch(AboutFiatCurrencyActivity.class);
    }

    @Override
    public void onRightImgClicked(View v) {
        super.onRightImgClicked(v);
        if (mCoinList.isEmpty()) {
            getCoinList();
        } else {
            showCoinListDialog();
        }
        /*RouterUtils.getInstance()
                .build(RouterConstants.PATH_MESSAGE_CENTER)
                .navigation();*/

    }


    @Override
    public void onRightLeftImgClicked(View v) {
        super.onRightLeftImgClicked(v);
       /* if (mCoinList.isEmpty()) {
            getCoinList();
        } else {
            showCoinListDialog();
        }*/

    }

    private SelectCoinWindow selectCoinWindow;
    private void showCoinListDialog() {
        if (selectCoinWindow==null){
            selectCoinWindow=new SelectCoinWindow(mContext,mCoinList);
        }
        selectCoinWindow.setOnSelectCoin(new SelectCoinWindow.OnSelectCoin() {
            @Override
            public void onSelectCoin(FiatCoin fiatCoin) {
                curfiatCoin=fiatCoin;
                updateData();
                selectCoinWindow.dismiss();

            }
        });
        selectCoinWindow.showAsDropDown(mBarRightImageView,-(selectCoinWindow.getWidth()/3*2),0);
    }

    private void updateData(){
        setCurrentTitle(curfiatCoin.getKey().toUpperCase());
        EventBus.getDefault().post(new BaseEvent<>(BaseEvent.EventCode.REFRESH_FIAT_COIN_NAME,curfiatCoin));

    }

    @Override
    protected void initData() {

    }


    @Override
    protected void onFirstUserVisible() {
        List<String> titles = new ArrayList<>();
        titles.add("买入");
        titles.add("卖出");
        titles.add("当前订单");
        titles.add("历史订单");
        setUpTitles(titles);
    }


    private void setUpTitles(List<String> titles) {

        fragmentList.add(TradeFiatCurrencyTradeFragment.newInstance(TYPE_BUY));
        fragmentList.add(TradeFiatCurrencyTradeFragment.newInstance(TYPE_SELL));
        fragmentList.add(TradeFiatCurrencyOrderFragment.newInstance(ORDER_CURRENT));
        fragmentList.add(TradeFiatCurrencyOrderFragment.newInstance(ORDER_HISTORY));
        viewPager.setAdapter(new BaseFragmentAdapter(getChildFragmentManager(), fragmentList, titles));
        tabLayout.setViewPager(viewPager);
        viewPager.setOffscreenPageLimit(titles.size());
        viewPager.setCurrentItem(0, false);

    }


    @Override
    protected int getLayoutId() {
        return R.layout.tmtrade_fragment_fiat_currency;
    }


    //----------------------------------------------网络请求---------------------------------------------------


    private void getCoinList() {
        mPresenter.getFiatCoinList();
    }

    @Override
    public void onGetFiatCoinListSuccess(List<FiatCoin> data) {
        mCoinList.clear();
        if (data != null) {
            mCoinList.addAll(data);
        }
        showCoinListDialog();
    }


    //----------------------------------------------event----------------------------------------------------

    @Subscriber
    void clearMsgNum(BaseEvent event) {
        /*if (event.getCode() == BaseEvent.EventCode.CLEAR_MSG) {
            badgeView.setBadgeNumber(0);
        }*/
    }

    @Subscriber
    void updateMsgNum(BaseEvent event) {
        /*if (event.getCode() == BaseEvent.EventCode.MSG_NUM) {
            HashMap<String, Integer> map = (HashMap<String, Integer>) event.getData();
            int num = 0;
            for (Integer integer : map.values()) {
                num += integer;
            }
            badgeView.setBadgeNumber(num);
        }*/
    }


}
